#!/usr/bin/env node
/**
 * 智能地理编码服务 - 结合BIM数据
 * 1. 使用高德地图获取小区基础位置
 * 2. 结合BIM数据中的建筑坐标进行精确定位
 */

const fs = require('fs').promises
const path = require('path')
const axios = require('axios')
const XLSX = require('xlsx')

function queryBimData() {
    var basePath = 'http://59.110.126.246:8085/mapdata/bimdata'
    var data = [
        {
            url: basePath + '/D1/tileset.json',
            lng: 120.62103879436891,
            lat: 30.162850171996755,
            x: -40,
            d: 'D1',
            title: 'D1',
            height: 25,
        },
        {
            url: basePath + '/D2/tileset.json',
            lng: 120.62142842468931,
            lat: 30.162572802462584,
            x: -42,
            d: 'D2',
            title: 'D2',
            height: 23,
        },
        {
            url: basePath + '/D3/tileset.json',
            lng: 120.62176075243005,
            lat: 30.16282320818289,
            x: -15,
            d: 'D3',
            title: 'D3',
            height: 23,
        },
        {
            url: basePath + '/D4/tileset.json',
            lng: 120.62200204829813,
            lat: 30.163060488118973,
            x: -10,
            d: 'D4',
            title: 'D4',
            height: 23,
        },
        {
            url: basePath + '/D5/tileset.json',
            lng: 120.62142613490892,
            lat: 30.16319945438769,
            x: -15,
            d: 'D5',
            title: 'D5',
            height: 23,
        },
        {
            url: basePath + '/D6/tileset.json',
            lng: 120.62221795967011,
            lat: 30.163295199142983,
            x: -15,
            d: 'D6',
            title: 'D6',
            height: 23,
        },
        {
            url: basePath + '/D7/tileset.json',
            lng: 120.62164818277462,
            lat: 30.163433719982642,
            x: -15,
            d: 'D7',
            title: 'D7',
            height: 25,
        },
        {
            url: basePath + '/D8/tileset.json',
            lng: 120.62242996215274,
            lat: 30.163525803392986,
            x: -15,
            d: 'D8',
            title: 'D8',
            height: 25,
        },
        {
            url: basePath + '/D9/tileset.json',
            lng: 120.6218703600572,
            lat: 30.163670831444627,
            x: -15,
            d: 'D9',
            title: 'D9',
            height: 25,
        },
        {
            url: basePath + '/D10/tileset.json',
            lng: 120.62271057507893,
            lat: 30.163740109838748,
            x: -15,
            d: 'D10',
            title: 'D10',
            height: 25,
        },
        {
            url: basePath + '/D11/tileset.json',
            lng: 120.62210357159597,
            lat: 30.16388858074902,
            x: -10,
            d: 'D11',
            title: 'D11',
            height: 25,
        },
        {
            url: basePath + '/D12/tileset.json',
            lng: 120.62237408731339,
            lat: 30.164128116697864,
            x: -15,
            d: 'D12',
            title: 'D12',
            height: 25,
        },
        {
            url: basePath + '/D13/tileset.json',
            lng: 120.62260349703965,
            lat: 30.164348212084473,
            x: -15,
            d: 'D13',
            title: 'D13',
            height: 23,
        },
        {
            url: basePath + '/D14/tileset.json',
            lng: 120.62297890176063,
            lat: 30.16424861582512,
            x: -15,
            d: 'D14',
            title: 'D14',
            height: 25,
        },
        {
            url: basePath + '/D17/tileset.json',
            lng: 120.62280173103721,
            lat: 30.164587108646704,
            x: -15,
            d: 'D17',
            title: 'D17',
            height: 23,
        },
        {
            url: basePath + '/D16/tileset.json',
            lng: 120.62333528771202,
            lat: 30.164455197655663,
            x: -15,
            d: 'D16',
            title: 'D16',
            height: 23,
        },
        {
            url: basePath + '/D19/tileset.json',
            lng: 120.6230459054544,
            lat: 30.16481495905548,
            x: -15,
            d: 'D19',
            title: 'D19',
            height: 23,
        },
        {
            url: basePath + '/D18/tileset.json',
            lng: 120.62357785125653,
            lat: 30.164687763444153,
            x: -15,
            d: 'D18',
            title: 'D18',
            height: 23,
        },
        {
            url: basePath + '/D21/tileset.json',
            lng: 120.62324133925291,
            lat: 30.16503903599516,
            x: -15,
            d: 'D21',
            title: 'D21',
            height: 23,
        },
        {
            url: basePath + '/D20/tileset.json',
            lng: 120.6238099678103,
            lat: 30.164921040859465,
            x: -15,
            d: 'D20',
            title: 'D20',
            height: 23,
        },
        {
            url: basePath + '/22/tileset.json',
            lng: 120.62348963159629,
            lat: 30.165329821956927,
            x: -15,
            d: 'D23',
            title: 'D23',
            height: 60,
        },
        {
            url: basePath + '/22/tileset.json',
            lng: 120.62406107892012,
            lat: 30.165148662457685,
            x: -15,
            d: 'D22',
            title: 'D22',
            height: 60,
        },
        {
            url: basePath + '/22/tileset.json',
            lng: 120.62391343729603,
            lat: 30.16572297008838,
            x: -15,
            d: 'D25',
            title: 'D25',
            height: 60,
        },
        {
            url: basePath + '/D24/tileset.json',
            lng: 120.62432977192059,
            lat: 30.165520513538812,
            x: -15,
            d: 'D24',
            title: 'D24',
            height: 67,
        },

        {
            url: basePath + '/B1/B1D1/tileset.json',
            lng: 120.6126159244893,
            lat: 30.159108293085133,
            x: -42,
            d: 'B1D1',
            title: 'D1',
            height: 25,
        },
        {
            url: basePath + '/B1/B1D2/tileset.json',
            lng: 120.61306604459931,
            lat: 30.15876565343761,
            x: -41,
            d: 'B1D2',
            title: 'D2',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D3/tileset.json',
            lng: 120.61424446592378,
            lat: 30.15786570192523,
            x: -40,
            d: 'B1D3',
            title: 'D3',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D4/tileset.json',
            lng: 120.61480826684785,
            lat: 30.157434755360315,
            x: -39,
            d: 'B1D4',
            title: 'D4',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D5/tileset.json',
            lng: 120.61271659397717,
            lat: 30.159368465073575,
            x: -39,
            d: 'B1D5',
            title: 'D5',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D6/tileset.json',
            lng: 120.61329179387341,
            lat: 30.159014581999275,
            x: -39,
            d: 'B1D6',
            title: 'D6',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D7/tileset.json',
            lng: 120.61453054029889,
            lat: 30.15808098038718,
            x: -39,
            d: 'B1D7',
            title: 'D7',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D8/tileset.json',
            lng: 120.6150411076277,
            lat: 30.157685712414238,
            x: -39,
            d: 'B1D8',
            title: 'D8',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D9/tileset.json',
            lng: 120.6129177980519,
            lat: 30.15959343004208,
            x: -39,
            d: 'B1D9',
            title: 'D9',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D10/tileset.json',
            lng: 120.61386255791948,
            lat: 30.158849670084702,
            x: -41,
            d: 'B1D10',
            title: 'D10',
            height: 25,
        },
        {
            url: basePath + '/B1/B1D11/tileset.json',
            lng: 120.61423292449332,
            lat: 30.158585582071478,
            x: -42,
            d: 'B1D11',
            title: 'D11',
            height: 25,
        },
        {
            url: basePath + '/B1/B1D12/tileset.json',
            lng: 120.61354005202638,
            lat: 30.159267162377013,
            x: -41,
            d: 'B1D12',
            title: 'D12',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D13/tileset.json',
            lng: 120.6147635881036,
            lat: 30.15836136514844,
            x: -39,
            d: 'B1D13',
            title: 'D13',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D14/tileset.json',
            lng: 120.61527510668985,
            lat: 30.15796050202005,
            x: -41,
            d: 'B1D14',
            title: 'D14',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D15/tileset.json',
            lng: 120.6130819314762,
            lat: 30.15982669709323,
            x: -41,
            d: 'B1D15',
            title: 'D15',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D16/tileset.json',
            lng: 120.61406800242486,
            lat: 30.159038753178397,
            x: -42,
            d: 'B1D16',
            title: 'D16',
            height: 25,
        },
        {
            url: basePath + '/B1/B1D17/tileset.json',
            lng: 120.61443269203237,
            lat: 30.158790531829574,
            x: -42,
            d: 'B1D17',
            title: 'D17',
            height: 25,
        },
        {
            url: basePath + '/B1/B1D18/tileset.json',
            lng: 120.61375920034432,
            lat: 30.159479134494585,
            x: -42,
            d: 'B1D18',
            title: 'D18',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D19/tileset.json',
            lng: 120.6149889601112,
            lat: 30.158568550774334,
            x: -40,
            d: 'B1D19',
            title: 'D19',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D14/tileset.json',
            lng: 120.6154609158684,
            lat: 30.15820848979066,
            x: -42,
            d: 'B1D20',
            title: 'D20',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D21/tileset.json',
            lng: 120.61324938990133,
            lat: 30.160050297915728,
            x: -41,
            d: 'B1D21',
            title: 'D21',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D22/tileset.json',
            lng: 120.614259171071,
            lat: 30.159247918029383,
            x: -42,
            d: 'B1D22',
            title: 'D22',
            height: 25,
        },
        {
            url: basePath + '/B1/B1D23/tileset.json',
            lng: 120.61463021366983,
            lat: 30.158983023954594,
            x: -41,
            d: 'B1D23',
            title: 'D23',
            height: 25,
        },
        {
            url: basePath + '/B1/B1D14/tileset.json',
            lng: 120.61333375000709,
            lat: 30.160374675335202,
            x: -42,
            d: 'B1D24',
            title: 'D24',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D5/tileset.json',
            lng: 120.61374301718337,
            lat: 30.160040555289484,
            x: -42,
            d: 'B1D25',
            title: 'D25',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D27/tileset.json',
            lng: 120.61420978423065,
            lat: 30.159666744882365,
            x: -42,
            d: 'B1D26',
            title: 'D26',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D27/tileset.json',
            lng: 120.61474422999412,
            lat: 30.159257829016205,
            x: -42,
            d: 'B1D27',
            title: 'D27',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D28/tileset.json',
            lng: 120.61522928364745,
            lat: 30.158861650437288,
            x: -42,
            d: 'B1D28',
            title: 'D28',
            height: 23,
        },
        {
            url: basePath + '/B1/B1D29/tileset.json',
            lng: 120.61568760860679,
            lat: 30.158510497071518,
            x: -42,
            d: 'B1D29',
            title: 'D29',
            height: 23,
        },

        {
            url: basePath + '/B2/B2D1/tileset.json',
            lng: 120.62161048061444,
            lat: 30.1612496989007,
            x: -30,
            d: 'B2D1',
            title: 'D1',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D2/tileset.json',
            lng: 120.62219727092571,
            lat: 30.160919979656875,
            x: -30,
            d: 'B2D2',
            title: 'D2',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D3/tileset.json',
            lng: 120.62284000712397,
            lat: 30.160598601281897,
            x: -30,
            d: 'B2D3',
            title: 'D3',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D4/tileset.json',
            lng: 120.62315972427533,
            lat: 30.160760242633533,
            x: -32,
            d: 'B2D4',
            title: 'D4',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D5/tileset.json',
            lng: 120.62169767804346,
            lat: 30.161523345601957,
            x: -33,
            d: 'B2D5',
            title: 'D5',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D6/tileset.json',
            lng: 120.62227116883969,
            lat: 30.1613272528858,
            x: -15,
            d: 'B2D6',
            title: 'D6',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D7/tileset.json',
            lng: 120.62286510208088,
            lat: 30.161175320749432,
            x: -15,
            d: 'B2D7',
            title: 'D7',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D8/tileset.json',
            lng: 120.62343130828565,
            lat: 30.16102387293419,
            x: -15,
            d: 'B2D8',
            title: 'D8',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D9/tileset.json',
            lng: 120.62195672369693,
            lat: 30.16177482817929,
            x: -15,
            d: 'B2D9',
            title: 'D9',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D10/tileset.json',
            lng: 120.62250566130425,
            lat: 30.16156368610037,
            x: -15,
            d: 'B2D10',
            title: 'D10',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D11/tileset.json',
            lng: 120.6230833888238,
            lat: 30.161436320021323,
            x: -15,
            d: 'B2D11',
            title: 'D11',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D12/tileset.json',
            lng: 120.62366476888468,
            lat: 30.161286615170802,
            x: -15,
            d: 'B2D12',
            title: 'D12',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D13/tileset.json',
            lng: 120.62216400413267,
            lat: 30.162031602347515,
            x: -14,
            d: 'B2D13',
            title: 'D13',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D14/tileset.json',
            lng: 120.62273316019825,
            lat: 30.161813248513717,
            x: -15,
            d: 'B2D14',
            title: 'D14',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D15/tileset.json',
            lng: 120.62330792197069,
            lat: 30.161667487687662,
            x: -15,
            d: 'B2D15',
            title: 'D15',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D16/tileset.json',
            lng: 120.62390123479614,
            lat: 30.161520425263035,
            x: -15,
            d: 'B2D16',
            title: 'D16',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D17/tileset.json',
            lng: 120.62240227882464,
            lat: 30.162268716261206,
            x: -15,
            d: 'B2D17',
            title: 'D17',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D18/tileset.json',
            lng: 120.62295797928917,
            lat: 30.16205090874039,
            x: -15,
            d: 'B2D18',
            title: 'D18',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D19/tileset.json',
            lng: 120.62354634386737,
            lat: 30.16190513236697,
            x: -15,
            d: 'B2D19',
            title: 'D19',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D20/tileset.json',
            lng: 120.62413044151047,
            lat: 30.16175461495602,
            x: -14,
            d: 'B2D20',
            title: 'D20',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D21/tileset.json',
            lng: 120.62265414443796,
            lat: 30.16250238257676,
            x: -15,
            d: 'B2D21',
            title: 'D21',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D22/tileset.json',
            lng: 120.62318875531453,
            lat: 30.162290254685438,
            x: -15,
            d: 'B2D22',
            title: 'D22',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D23/tileset.json',
            lng: 120.62379026290924,
            lat: 30.16214563445681,
            x: -15,
            d: 'B2D23',
            title: 'D23',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D24/tileset.json',
            lng: 120.62435929131594,
            lat: 30.161999046162027,
            x: -15,
            d: 'B2D24',
            title: 'D24',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D25/tileset.json',
            lng: 120.62342520727238,
            lat: 30.162533968903006,
            x: -15,
            d: 'B2D25',
            title: 'D25',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D26/tileset.json',
            lng: 120.62400941551853,
            lat: 30.16239458194812,
            x: -15,
            d: 'B2D26',
            title: 'D26',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D27/tileset.json',
            lng: 120.62459422020565,
            lat: 30.162223205073094,
            x: -15,
            d: 'B2D27',
            title: 'D27',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D28/tileset.json',
            lng: 120.62424245510701,
            lat: 30.16262269360602,
            x: -15,
            d: 'B2D28',
            title: 'D28',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D29/tileset.json',
            lng: 120.62483384633087,
            lat: 30.162476543035407,
            x: -15,
            d: 'B2D29',
            title: 'D29',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D30/tileset.json',
            lng: 120.62394637758133,
            lat: 30.163011329865114,
            x: -15,
            d: 'B2D30',
            title: 'D30',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D31/tileset.json',
            lng: 120.62453198583502,
            lat: 30.16284752218056,
            x: -15,
            d: 'B2D31',
            title: 'D31',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D32/tileset.json',
            lng: 120.62508227691794,
            lat: 30.16274817073483,
            x: -15,
            d: 'B2D32',
            title: 'D32',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D33/tileset.json',
            lng: 120.62413250651143,
            lat: 30.163252666725082,
            x: -15,
            d: 'B2D33',
            title: 'D33',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D34/tileset.json',
            lng: 120.6247461062742,
            lat: 30.163080348037983,
            x: -15,
            d: 'B2D34',
            title: 'D34',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D35/tileset.json',
            lng: 120.62531801522658,
            lat: 30.16298254301844,
            x: -15,
            d: 'B2D35',
            title: 'D35',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D36/tileset.json',
            lng: 120.62442819679046,
            lat: 30.163476142018986,
            x: -15,
            d: 'B2D36',
            title: 'D36',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D37/tileset.json',
            lng: 120.62499364291116,
            lat: 30.163308712466623,
            x: -15,
            d: 'B2D37',
            title: 'D37',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D38/tileset.json',
            lng: 120.6255570071543,
            lat: 30.163219027223374,
            x: -15,
            d: 'B2D38',
            title: 'D38',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D39/tileset.json',
            lng: 120.62401543976314,
            lat: 30.163929118505312,
            x: -15,
            d: 'B2D39',
            title: 'D39',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D40/tileset.json',
            lng: 120.62458768589642,
            lat: 30.16372248537528,
            x: -15,
            d: 'B2D40',
            title: 'D40',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D41/tileset.json',
            lng: 120.62515575265519,
            lat: 30.163548406881112,
            x: -15,
            d: 'B2D41',
            title: 'D41',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D42/tileset.json',
            lng: 120.62577902591109,
            lat: 30.163457955851538,
            x: -15,
            d: 'B2D42',
            title: 'D42',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D43/tileset.json',
            lng: 120.62423044073042,
            lat: 30.164156844595928,
            x: -15,
            d: 'B2D43',
            title: 'D43',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D44/tileset.json',
            lng: 120.62485345784047,
            lat: 30.163942774206298,
            x: -15,
            d: 'B2D44',
            title: 'D44',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D45/tileset.json',
            lng: 120.62542108998042,
            lat: 30.163751113466176,
            x: -15,
            d: 'B2D45',
            title: 'D45',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D46/tileset.json',
            lng: 120.62449132473637,
            lat: 30.164384616348205,
            x: -20,
            d: 'B2D46',
            title: 'D46',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D47/tileset.json',
            lng: 120.62502766783409,
            lat: 30.16417650164807,
            x: -15,
            d: 'B2D47',
            title: 'D47',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D48/tileset.json',
            lng: 120.62552533549727,
            lat: 30.163988898191803,
            x: -15,
            d: 'B2D48',
            title: 'D48',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D49/tileset.json',
            lng: 120.62600668832522,
            lat: 30.163727828606604,
            x: -40,
            d: 'B2D49',
            title: 'D49',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D50/tileset.json',
            lng: 120.6248001665347,
            lat: 30.16456543606539,
            x: -20,
            d: 'B2D50',
            title: 'D50',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D51/tileset.json',
            lng: 120.62543147978072,
            lat: 30.16434837343861,
            x: -20,
            d: 'B2D51',
            title: 'D51',
            height: 25,
        },
        {
            url: basePath + '/B2/B2D52/tileset.json',
            lng: 120.62591177720621,
            lat: 30.164142370718785,
            x: -33,
            d: 'B2D52',
            title: 'D52',
            height: 23,
        },
        {
            url: basePath + '/B2/B2D53/tileset.json',
            lng: 120.62625224643494,
            lat: 30.163910084031777,
            x: -40,
            d: 'B2D53',
            title: 'D53',
            height: 25,
        },

        {
            url: basePath + '/B3/B3D1/tileset.json',
            lng: 120.62562604166238,
            lat: 30.16232011640382,
            x: -11,
            d: 'B3D1',
            title: 'D1',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D2/tileset.json',
            lng: 120.62566263496957,
            lat: 30.162016413878394,
            x: -13,
            d: 'B3D2',
            title: 'D2',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D3/tileset.json',
            lng: 120.62616963624497,
            lat: 30.161904162612835,
            x: -15,
            d: 'B3D3',
            title: 'D3',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D4/tileset.json',
            lng: 120.62518811130143,
            lat: 30.161865945658192,
            x: -13,
            d: 'B3D4',
            title: 'D4',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D5/tileset.json',
            lng: 120.6256549156256,
            lat: 30.16172809014637,
            x: -15,
            d: 'B3D5',
            title: 'D5',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D6/tileset.json',
            lng: 120.62605974602367,
            lat: 30.161628558842196,
            x: -14,
            d: 'B3D6',
            title: 'D6',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D7/tileset.json',
            lng: 120.62664209847871,
            lat: 30.161488920607177,
            x: -15,
            d: 'B3D7',
            title: 'D7',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D8/tileset.json',
            lng: 120.62501708886901,
            lat: 30.161611895608306,
            x: -14,
            d: 'B3D8',
            title: 'D8',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D9/tileset.json',
            lng: 120.62554639968216,
            lat: 30.1614575743428,
            x: -15,
            d: 'B3D9',
            title: 'D9',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D10/tileset.json',
            lng: 120.62597064561723,
            lat: 30.16134503542695,
            x: -14,
            d: 'B3D10',
            title: 'D10',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D11/tileset.json',
            lng: 120.6265058831453,
            lat: 30.161222957389672,
            x: -14,
            d: 'B3D11',
            title: 'D11',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D12/tileset.json',
            lng: 120.62487036276114,
            lat: 30.161348773337483,
            x: -14,
            d: 'B3D12',
            title: 'D12',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D13/tileset.json',
            lng: 120.62546481582902,
            lat: 30.161170659025398,
            x: -14,
            d: 'B3D13',
            title: 'D13',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D14/tileset.json',
            lng: 120.62590510181268,
            lat: 30.16107090397789,
            x: -14,
            d: 'B3D14',
            title: 'D14',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D15/tileset.json',
            lng: 120.62640254981297,
            lat: 30.16096097321143,
            x: -14,
            d: 'B3D15',
            title: 'D15',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D16/tileset.json',
            lng: 120.62451614692044,
            lat: 30.16113097937685,
            x: -14,
            d: 'B3D16',
            title: 'D16',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D17/tileset.json',
            lng: 120.62490395379234,
            lat: 30.16106781095135,
            x: -14,
            d: 'B3D17',
            title: 'D17',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D18/tileset.json',
            lng: 120.6254930166536,
            lat: 30.16082036762008,
            x: -15,
            d: 'B3D18',
            title: 'D18',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D19/tileset.json',
            lng: 120.62429365513982,
            lat: 30.160930640824086,
            x: -14,
            d: 'B3D19',
            title: 'D19',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D20/tileset.json',
            lng: 120.62470488539192,
            lat: 30.160802552553406,
            x: -15,
            d: 'B3D20',
            title: 'D20',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D21/tileset.json',
            lng: 120.62528393051893,
            lat: 30.160575778510103,
            x: -14,
            d: 'B3D21',
            title: 'D21',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D22/tileset.json',
            lng: 120.62569569754869,
            lat: 30.16047203132741,
            x: -12,
            d: 'B3D22',
            title: 'D22',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D23/tileset.json',
            lng: 120.62417900618367,
            lat: 30.16065554317707,
            x: -13,
            d: 'B3D23',
            title: 'D23',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D24/tileset.json',
            lng: 120.62469123067523,
            lat: 30.160533627261945,
            x: -13,
            d: 'B3D24',
            title: 'D24',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D25/tileset.json',
            lng: 120.62520053456744,
            lat: 30.160301857461474,
            x: -14,
            d: 'B3D25',
            title: 'D25',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D26/tileset.json',
            lng: 120.62559779390304,
            lat: 30.160199940447494,
            x: -13,
            d: 'B3D26',
            title: 'D26',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D27/tileset.json',
            lng: 120.62396536464917,
            lat: 30.16041521672736,
            x: -14,
            d: 'B3D27',
            title: 'D27',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D28/tileset.json',
            lng: 120.62456770072241,
            lat: 30.16028110399299,
            x: -14,
            d: 'B3D28',
            title: 'D28',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D29/tileset.json',
            lng: 120.62510906327918,
            lat: 30.160037465750023,
            x: -14,
            d: 'B3D29',
            title: 'D29',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D30/tileset.json',
            lng: 120.62562460132045,
            lat: 30.15990698438988,
            x: -14,
            d: 'B3D30',
            title: 'D30',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D31/tileset.json',
            lng: 120.62375531005359,
            lat: 30.16017879629334,
            x: -14,
            d: 'B3D31',
            title: 'D31',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D32/tileset.json',
            lng: 120.62436327595447,
            lat: 30.160038142992336,
            x: -14,
            d: 'B3D32',
            title: 'D32',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D33/tileset.json',
            lng: 120.6249780739416,
            lat: 30.159771607443082,
            x: -14,
            d: 'B3D33',
            title: 'D33',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D34/tileset.json',
            lng: 120.62545059854405,
            lat: 30.159661493718078,
            x: -14,
            d: 'B3D34',
            title: 'D34',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D35/tileset.json',
            lng: 120.62433985967588,
            lat: 30.15974500542778,
            x: -14,
            d: 'B3D35',
            title: 'D35',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D36/tileset.json',
            lng: 120.62478124785563,
            lat: 30.159530491189203,
            x: -14,
            d: 'B3D36',
            title: 'D36',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D37/tileset.json',
            lng: 120.62522247231287,
            lat: 30.159415872262227,
            x: -14,
            d: 'B3D37',
            title: 'D37',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D38/tileset.json',
            lng: 120.62506589044809,
            lat: 30.159180078710044,
            x: -14,
            d: 'B3D38',
            title: 'D38',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D39/tileset.json',
            lng: 120.62668125345729,
            lat: 30.159970434305704,
            x: -14,
            d: 'B3D39',
            title: 'D39',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D40/tileset.json',
            lng: 120.62707841451565,
            lat: 30.159819040558128,
            x: -14,
            d: 'B3D40',
            title: 'D40',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D41/tileset.json',
            lng: 120.62643516055736,
            lat: 30.1597408962677,
            x: -14,
            d: 'B3D41',
            title: 'D41',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D42/tileset.json',
            lng: 120.62688191482079,
            lat: 30.159575290140573,
            x: -14,
            d: 'B3D42',
            title: 'D42',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D43/tileset.json',
            lng: 120.62746915467666,
            lat: 30.159460995259595,
            x: -14,
            d: 'B3D43',
            title: 'D43',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D44/tileset.json',
            lng: 120.62627673820583,
            lat: 30.159477956418155,
            x: -14,
            d: 'B3D44',
            title: 'D44',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D45/tileset.json',
            lng: 120.62679853909805,
            lat: 30.15930006307355,
            x: -14,
            d: 'B3D45',
            title: 'D45',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D46/tileset.json',
            lng: 120.62732898199118,
            lat: 30.159203844241313,
            x: -14,
            d: 'B3D46',
            title: 'D46',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D47/tileset.json',
            lng: 120.62618644980581,
            lat: 30.159204006872264,
            x: -14,
            d: 'B3D47',
            title: 'D47',
            height: 25,
        },
        {
            url: basePath + '/B3/B3D48/tileset.json',
            lng: 120.62657971313056,
            lat: 30.159035654583982,
            x: -14,
            d: 'B3D48',
            title: 'D48',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D49/tileset.json',
            lng: 120.62706140630134,
            lat: 30.15892798555539,
            x: -14,
            d: 'B3D49',
            title: 'D49',
            height: 23,
        },
        {
            url: basePath + '/B3/B3D50/tileset.json',
            lng: 120.62689780520037,
            lat: 30.15868091926733,
            x: -14,
            d: 'B3D50',
            title: 'D50',
            height: 23,
        },
    ]
    return data
}

// 配置日志
class Logger {
    constructor() {
        this.logFile = 'geocoding_with_bim.log'
    }

    async log(level, message) {
        const timestamp = new Date().toISOString()
        const logMessage = `${timestamp} - ${level} - ${message}\n`

        console.log(logMessage.trim())

        try {
            await fs.appendFile(this.logFile, logMessage, 'utf8')
        } catch (err) {
            console.error('写入日志文件失败:', err)
        }
    }

    info(message) {
        return this.log('INFO', message)
    }

    error(message) {
        return this.log('ERROR', message)
    }

    warning(message) {
        return this.log('WARNING', message)
    }
}

const logger = new Logger()

/**
 * 从common.js提取BIM数据
 */
class BIMDataExtractor {
    /**
     * 提取BIM建筑坐标数据
     */
    static async extractBimCoordinates() {
        const bimData = {}

        try {
            const content = '' //await fs.readFile('common.js', 'utf8');

            // 提取queryBimData函数中的数据
            // 查找包含坐标的数据
            const pattern1 =
                /lng:([\d.]+),\s*lat:([\d.]+),.*?d:'([^']+)',.*?title:'([^']+)'/g
            let match

            while ((match = pattern1.exec(content)) !== null) {
                const [, lng, lat, dCode, title] = match
                bimData[dCode] = {
                    longitude: parseFloat(lng),
                    latitude: parseFloat(lat),
                    title: title,
                }
            }

            // 提取queryBuildingData函数中的详细房间数据
            const pattern2 = /"lat":([\d.]+),"lng":([\d.]+),.*?"id":"([^"]+)"/g

            while ((match = pattern2.exec(content)) !== null) {
                const [, lat, lng, roomId] = match
                bimData[roomId] = {
                    longitude: parseFloat(lng),
                    latitude: parseFloat(lat),
                    roomId: roomId,
                }
            }

            await logger.info(
                `成功提取 ${Object.keys(bimData).length} 条BIM坐标数据`
            )
            return bimData
        } catch (error) {
            await logger.error(`提取BIM数据失败: ${error.message}`)
            return {}
        }
    }
}

/**
 * 智能地理编码器 - 结合高德地图和BIM数据
 */
class SmartGeocoder {
    constructor(apiKey) {
        this.apiKey = apiKey
        this.baseUrl = 'https://restapi.amap.com/v3/geocode/geo'
        this.cache = new Map()
        this.bimData = {}
        // this.initializeBimData()
    }

    // async initializeBimData() {
    //     this.bimData = await BIMDataExtractor.extractBimCoordinates()
    // }

    /**
     * 获取小区的基础坐标
     */
    async geocodeCommunity(communityName) {
        if (this.cache.has(communityName)) {
            return this.cache.get(communityName)
        }

        const params = {
            key: this.apiKey,
            address: communityName,
            output: 'json',
        }

        try {
            const response = await axios.get(this.baseUrl, {
                params: params,
                timeout: 10000,
            })
            //120.625196,30.160210 //兴海嘉苑1幢
            //'120.625600,30.159911'  //兴海嘉苑2幢1单元
            const data = response.data

            if (data.status === '1' && data.count !== '0') {
                const geocode = data.geocodes[0]
                const location = geocode.location.split(',')

                const result = {
                    success: true,
                    longitude: parseFloat(location[0]),
                    latitude: parseFloat(location[1]),
                    formatted_address: geocode.formatted_address || '',
                    province: geocode.province || '',
                    city: geocode.city || '',
                    district: geocode.district || '',
                }

                this.cache.set(communityName, result)
                return result
            } else {
                return { success: false }
            }
        } catch (error) {
            await logger.error(`地理编码失败: ${error.message}`)
            return { success: false }
        }
    }

    /**
     * 根据建筑编号获取精确坐标
     */
    getBuildingCoordinates(buildingNumber, roomNumber = null) {
        // 1. 首先尝试精确匹配房间ID
        if (roomNumber) {
            // 构建可能的房间ID格式
            const possibleIds = [
                `兴海嘉苑-${buildingNumber}-${roomNumber}`,
                `安滨嘉苑-${buildingNumber}-${roomNumber}`,
                `${buildingNumber}-${roomNumber}`,
            ]

            for (const roomId of possibleIds) {
                if (this.bimData[roomId]) {
                    return this.bimData[roomId]
                }
            }
        }

        // 2. 尝试匹配建筑编号
        if (this.bimData[buildingNumber]) {
            return this.bimData[buildingNumber]
        }

        // 3. 尝试模糊匹配
        for (const [key, data] of Object.entries(this.bimData)) {
            if (key.includes(buildingNumber)) {
                return data
            }
        }

        return null
    }

    /**
     * 计算建筑物相对于小区中心的偏移量
     */
    calculateOffset(baseLat, baseLng, buildingNumber, index) {
        // 根据建筑编号计算偏移
        // 这是一个简单的算法，实际应用中可能需要更复杂的布局算法

        // 提取建筑编号中的数字
        const match = buildingNumber.match(/(\d+)/)
        const buildingNum = match ? parseInt(match[1]) : index

        // 计算行列位置（假设建筑按网格排列）
        const row = Math.floor((buildingNum - 1) / 10)
        const col = (buildingNum - 1) % 10

        // 每栋楼之间的间距（经纬度）
        const spacingLat = 0.0002 // 约22米
        const spacingLng = 0.0003 // 约30米

        // 计算偏移后的坐标
        const offsetLat = baseLat + (row - 5) * spacingLat
        const offsetLng = baseLng + (col - 5) * spacingLng

        return [offsetLat, offsetLng]
    }
}

/**
 * 处理房产数据 - 结合BIM坐标
 */
async function processHouseDataWithBim(apiKey) {
    await logger.info('开始处理房产权属数据（结合BIM数据）...')

    // 读取Excel文件
    let jsonData

    // try {
    // workbook = XLSX.readFile('house_owner.xls');
    // const sheetName = workbook.SheetNames[0];
    // worksheet = workbook.Sheets[sheetName];
    // jsonData = XLSX.utils.sheet_to_json(worksheet);
    jsonData = [
        {
            id: 1,
            room_number: '101',
            building_number: 'D1',
            actual_resident: '张小龙',
            number: '1018900',
            relationship: '户主',
            tel: '13625751385',
            registered_residence: '新海社区',
            work_unit: '绍兴伽瑞印染有限公司',
            xqname: '兴海嘉苑',
            longitude: null,
            latitude: null,
            geocode_method: '',
            geocode_confidence: 0,
            formatted_address: '',
            province: '',
            city: '',
            district: '',
        },
    ]

    //   await logger.info(`成功读取 ${jsonData.length} 条记录`)
    // await logger.info(`列名: ${Object.keys(jsonData[0] || {}).join(', ')}`)
    // } catch (error) {
    //     await logger.error(`读取Excel文件失败: ${error.message}`)
    //     return
    // }

    // 创建智能地理编码器
    const geocoder = new SmartGeocoder(apiKey)

    // await geocoder.initializeBimData()

    // 初始化结果列
    // jsonData.forEach(row => {
    //     row.longitude = null
    //     row.latitude = null
    //     row.geocode_method = '' // 记录获取坐标的方法
    //     row.geocode_confidence = 0.0
    //     row.formatted_address = ''
    //     row.province = ''
    //     row.city = ''
    //     row.district = ''
    // })

    // 统计
    const stats = {
        bim_exact: 0, // BIM精确匹配
        bim_building: 0, // BIM建筑匹配
        calculated: 0, // 计算得出
        community_center: 0, // 小区中心点
        failed: 0, // 失败
    }

    // 获取小区基础坐标
    let communityResult = null
    const req = jsonData.map(async item => {
        if ('xqname' in item) {
            return await geocoder.geocodeCommunity(item.xqname)
        }
        return null
    })
    /**
     * [
     * {
  success: true,
  longitude: 120.626893,
  latitude: 30.161586,
  formatted_address: "浙江省绍兴市柯桥区兴海嘉苑",
  province: "浙江省",
  city: "绍兴市",
  district: "柯桥区",
}]
     */
    const res = await Promise.all(req)
    console.log(res)
    debugger

    // if (jsonData.length > 0 && jsonData[0].xqname) {
    //     const xqname = jsonData[0].xqname
    //     if (xqname) {
    //         await logger.info(`获取小区 '${xqname}' 的基础坐标...`)
    //         communityResult = await geocoder.geocodeCommunity('兴海嘉苑2幢')
    //         if (communityResult.success) {
    //             await logger.info(
    //                 `小区中心坐标: (${communityResult.longitude}, ${communityResult.latitude})`
    //             )
    //         }
    //     }
    // }

    // 处理每条记录
    for (let index = 0; index < jsonData.length; index++) {
        const row = jsonData[index]
        await logger.info(`\n处理 [${index + 1}/${jsonData.length}]:`)

        const buildingNumber = String(row.building_number || '').trim()
        const roomNumber = String(row.room_number || '').trim()

        // 1. 尝试从BIM数据获取精确坐标
        const bimCoords = geocoder.getBuildingCoordinates(
            buildingNumber,
            roomNumber
        )

        if (bimCoords) {
            row.longitude = bimCoords.longitude
            row.latitude = bimCoords.latitude
            row.geocode_method = roomNumber ? 'BIM精确匹配' : 'BIM建筑匹配'
            row.geocode_confidence = roomNumber ? 1.0 : 0.9

            if (roomNumber) {
                stats.bim_exact++
            } else {
                stats.bim_building++
            }

            await logger.info(
                `  ${buildingNumber} ${roomNumber} -> BIM坐标: (${bimCoords.longitude}, ${bimCoords.latitude})`
            )

            // 2. 如果没有BIM数据，使用小区中心点+偏移计算
        } else if (
            communityResult &&
            communityResult.success &&
            buildingNumber
        ) {
            const [offsetLat, offsetLng] = geocoder.calculateOffset(
                communityResult.latitude,
                communityResult.longitude,
                buildingNumber,
                index
            )

            row.longitude = offsetLng
            row.latitude = offsetLat
            row.geocode_method = '基于小区中心计算'
            row.geocode_confidence = 0.7
            stats.calculated++

            await logger.info(
                `  ${buildingNumber} -> 计算坐标: (${offsetLng}, ${offsetLat})`
            )

            // 3. 使用小区中心点
        } else if (communityResult && communityResult.success) {
            row.longitude = communityResult.longitude
            row.latitude = communityResult.latitude
            row.geocode_method = '小区中心点'
            row.geocode_confidence = 0.5
            stats.community_center++

            await logger.info(
                `  使用小区中心点: (${communityResult.longitude}, ${communityResult.latitude})`
            )
        } else {
            row.geocode_method = '失败'
            row.geocode_confidence = 0.0
            stats.failed++
            await logger.warning(`  无法获取坐标`)
        }

        // 填充地址信息
        if (communityResult && communityResult.success) {
            row.formatted_address = communityResult.formatted_address || ''
            row.province = communityResult.province || ''
            row.city = communityResult.city || ''
            row.district = communityResult.district || ''
        }
    }

    // 保存结果
    const timestamp = new Date()
        .toISOString()
        .replace(/[:.]/g, '-')
        .slice(0, 19)
    const outputFile = `house_owner_geocoded_bim_${timestamp}.xlsx`

    const newWorkbook = XLSX.utils.book_new()
    const newWorksheet = XLSX.utils.json_to_sheet(jsonData)
    XLSX.utils.book_append_sheet(newWorkbook, newWorksheet, 'Sheet1')
    XLSX.writeFile(newWorkbook, outputFile)

    await logger.info(`\n结果已保存到: ${outputFile}`)

    // 生成详细报告
    const report = `
地理编码完成报告（结合BIM数据）
================================
处理时间: ${new Date().toLocaleString()}
总记录数: ${jsonData.length}

坐标获取方式统计:
  BIM精确匹配: ${stats.bim_exact} 条
  BIM建筑匹配: ${stats.bim_building} 条
  基于小区计算: ${stats.calculated} 条
  小区中心点: ${stats.community_center} 条
  失败: ${stats.failed} 条

成功率: ${(((jsonData.length - stats.failed) / jsonData.length) * 100).toFixed(
        2
    )}%
`

    // 添加坐标范围统计
    const validData = jsonData.filter(row => row.longitude !== null)
    if (validData.length > 0) {
        const longitudes = validData.map(row => row.longitude)
        const latitudes = validData.map(row => row.latitude)
        const lonRange = [Math.min(...longitudes), Math.max(...longitudes)]
        const latRange = [Math.min(...latitudes), Math.max(...latitudes)]

        const additionalReport = `
坐标范围:
  经度: ${lonRange[0].toFixed(6)} ~ ${lonRange[1].toFixed(6)}
  纬度: ${latRange[0].toFixed(6)} ~ ${latRange[1].toFixed(6)}
  
坐标分布:
  不同经度值: ${new Set(longitudes).size} 个
  不同纬度值: ${new Set(latitudes).size} 个
`

        const finalReport = report + additionalReport

        // 保存报告
        const reportFile = `house_owner_geocoded_bim_${timestamp}_report.txt`
        await fs.writeFile(reportFile, finalReport, 'utf8')

        await logger.info(`报告已保存到: ${reportFile}`)
        await logger.info('\n' + finalReport)

        // 显示部分结果示例
        await logger.info('\n结果示例（前10条不同坐标）:')
        const uniqueCoords = new Map()
        const sampleData = []

        for (const row of validData) {
            const key = `${row.longitude},${row.latitude}`
            if (!uniqueCoords.has(key) && sampleData.length < 10) {
                uniqueCoords.set(key, true)
                sampleData.push(row)
            }
        }

        for (const row of sampleData) {
            await logger.info(
                `  ${row.building_number || ''} ${row.room_number || ''} -> ` +
                    `(${row.longitude.toFixed(6)}, ${row.latitude.toFixed(
                        6
                    )}) - ${row.geocode_method}`
            )
        }
    }
}

/**
 * 主函数
 */
async function main() {
    const API_KEY = '64fdb23b96e7a2c0c94c95a789ecef50'

    await logger.info('=' * 60)
    await logger.info('智能地理编码服务 - 结合BIM数据')
    await logger.info('=' * 60)
    // debugger
    // 检查必要文件
    // try {
    //     await fs.access('house_owner.xls')
    // } catch (error) {
    //     await logger.error('找不到 house_owner.xls 文件！')
    //     return
    // }

    // try {
    //     await fs.access('common.js')
    // } catch (error) {
    //     await logger.error('找不到 common.js 文件！')
    //     return
    // }

    // 处理数据
    await processHouseDataWithBim(API_KEY)
}

// 运行主函数
if (require.main === module) {
    main().catch(error => {
        console.error('程序执行出错:', error)
        process.exit(1)
    })
}

module.exports = {
    BIMDataExtractor,
    SmartGeocoder,
    processHouseDataWithBim,
}
